package com.lee.serviceagent.module.employee.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lee.serviceagent.common.domain.PageResult;
import com.lee.serviceagent.common.domain.ResponseDTO;
import com.lee.serviceagent.common.util.SmartBeanUtil;
import com.lee.serviceagent.common.util.SmartPageUtil;
import com.lee.serviceagent.module.employee.dao.EmployeeDao;

import com.lee.serviceagent.module.employee.domain.entity.EmployeeEntity;
import com.lee.serviceagent.module.employee.domain.form.EmployeeAddForm;
import com.lee.serviceagent.module.employee.domain.form.EmployeeQueryForm;
import com.lee.serviceagent.module.employee.domain.form.EmployeeUpdateForm;
import com.lee.serviceagent.module.employee.domain.vo.EmployeeVO;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *   Service
 *
 * @Author lee
 * @Date 2024-09-12 17:11:30
 * @Copyright lee
 */

@Service
@Slf4j
public class EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;


    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<EmployeeVO> queryPage(EmployeeQueryForm queryForm) {
        log.info("test traceId(Service)");
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<EmployeeVO> list = employeeDao.queryPage(page, queryForm);
        PageResult<EmployeeVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(EmployeeAddForm addForm) {
        EmployeeEntity employeeEntity = SmartBeanUtil.copy(addForm, EmployeeEntity.class);
        employeeDao.insert(employeeEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(EmployeeUpdateForm updateForm) {
        EmployeeEntity employeeEntity = SmartBeanUtil.copy(updateForm, EmployeeEntity.class);
        employeeDao.updateById(employeeEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        employeeDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        employeeDao.deleteById(id);
        return ResponseDTO.ok();
    }

}
